#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: tpfv1_datasource.py 
@version:
@time: 2022/01/05 
"""
from TapClientCore.T06_Tapdata_feature.v1.TPF01_datasource.tpf_datasource_base import V1TPFDataSourceBase
from TapException.TM import *


class TPFV1DataSource(V1TPFDataSourceBase):
    # 复制
    def copy_datasource_by_db_info(self, db_info):
        name = db_info.get('name')
        return self.TMFDataSource.copy_datasource_by_name(name)

    # 创建
    def create_datasource_by_db_info(self, db_info):
        name = db_info.get('name')
        connection_id = None
        tm_response = None
        mongodb_response = []
        try:
            tm_response = self.TMFDataSource.query_datasource_by_name(name)
        except TapdataNoThisDataSource:
            tm_response = {}
        # 查询中间库
        mongodb_response = self.datasource_query_mongodb(db_info)

        connection_id = tm_response.get('id')

        if not mongodb_response:
            if not tm_response:
                create_response = self.TMFDataSource.create_datasource(db_info)
                self.datasource_insert_mongodb(db_info, create_response.get('id'))
            else:
                self.datasource_insert_mongodb(db_info, connection_id)
        else:
            if not tm_response:
                self.datasource_delete_mongodb(db_info)
                create_response = self.TMFDataSource.create_datasource(db_info)
                self.datasource_insert_mongodb(db_info, create_response.get('id'))
            else:
                if connection_id == mongodb_response.get('id'):
                    pass
                else:
                    self.datasource_delete_mongodb(db_info)
                    self.datasource_insert_mongodb(db_info, connection_id)
        return connection_id

    # 删除
    def delete_datasource_by_db_info(self, db_info):
        self.TMFDataSource.delete_datasource_by_name(db_info.get('name'))
        self.datasource_delete_mongodb(db_info)

    # 修改
    def update_datasource_by_db_info(self, db_info, update):
        name = db_info.get('name')
        connection_id = None
        tm_response = None
        mongodb_response = []
        try:
            tm_response = self.TMFDataSource.query_datasource_by_name(name)
        except TapdataNoThisDataSource:
            tm_response = {}
        # 查询中间库
        mongodb_response = self.datasource_query_mongodb(db_info)
        connection_id = tm_response.get('id')
        tmp = db_info.copy()
        db_info.update(update)

        if not mongodb_response:
            if not tm_response:
                create_response = self.TMFDataSource.create_datasource(db_info)
                self.datasource_insert_mongodb(db_info, create_response.get('id'))
            else:
                self.TMFDataSource.patch_datasource_by_id(connection_id, db_info)
                self.datasource_insert_mongodb(db_info, connection_id)
        else:
            if not tm_response:
                self.datasource_delete_mongodb(db_info)
                create_response = self.TMFDataSource.create_datasource(db_info)
                self.datasource_insert_mongodb(db_info, create_response.get('id'))
            else:
                if connection_id == mongodb_response.get('id'):
                    self.TMFDataSource.patch_datasource_by_id(connection_id, db_info)
                    self.datasource_update_mongodb(tmp, connection_id, db_info)
                else:
                    self.datasource_delete_mongodb(tmp)
                    self.TMFDataSource.patch_datasource_by_id(connection_id, db_info)
                    self.datasource_insert_mongodb(db_info, connection_id)

    # 查询
    def query_datasource_by_db_info(self, db_info):
        tm_response = self.TMFDataSource.query_datasource_by_name(db_info.get('name'))
        from TapUtil.logfunc.logger import logger
        logger.info(f'id: {tm_response.get("id")}')
        logger.info(f'name: {tm_response.get("name")}')
        logger.info(f'more: {tm_response}')
        self.datasource_query_mongodb(db_info)
        return tm_response

    # load schema
    def load_datasource_schema_by_db_info(self, db_info):
        name = db_info.get('name')
        self._datasource_load_schema(name)

    # test connection + load schema
    def datasource_test_schema_by_db_info(self, db_info):
        name = db_info.get('name')
        self._datasource_test(name)


if __name__ == '__main__':
    from db_info import Mysql

    T = TPFV1DataSource()
    T.load_datasource_schema_by_db_info(Mysql.mysql_source_test)
